//
//  SearchViewController.m
//  DaJiang
//
//  Created by sally on 16/9/29.
//  Copyright © 2016年 jiniu. All rights reserved.
//

#import "SearchHistoryCell.h"
#import "SearchViewController.h"

@interface SearchViewController () <UISearchBarDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIView *headerView;
@property (weak, nonatomic) IBOutlet UIView *footerView;
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;

@end

@implementation SearchViewController

- (void)viewDidLoad {
  [super viewDidLoad];
  //需求修改，该页面已废弃
  [self initControl];
  [self initData];
}

#pragma mark 初始化控件
- (void)initControl {
  [self setNav];
  self.headerView.height = 30;
  //title 30；标签 30，最后bottom 10，间距 10
  self.footerView.height = 120;
  [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"Cell"];
}

#pragma mark 初始化数据
- (void)initData {
}

#pragma mark - TableView data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  return 2;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
  return cell.frame.size.height;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  SearchHistoryCell *cell = (SearchHistoryCell *) [tableView dequeueReusableCellWithIdentifier:@"SearchHistoryCell"];
  if (!cell) {
    cell = [[[NSBundle mainBundle] loadNibNamed:@"SearchHistoryCell" owner:nil options:nil] lastObject];
  }

  return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  [tableView deselectRowAtIndexPath:indexPath animated:YES]; //返回后选中状态消失
}

#pragma mark UICollectionView delegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
  return 2;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  return 4;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
  return CGSizeMake((ScreenWidth-30-30)/4, 30);
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];

  UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, cell.width, cell.height)];
  label.text = @"搜索";
  label.font = GCustomNormalFont;
  label.textColor = GGrayWordColor1;
  label.textAlignment = NSTextAlignmentCenter;
  
  for (id subView in cell.contentView.subviews) {
    [subView removeFromSuperview];
  }
  [cell.contentView addSubview:label];
  cell.backgroundColor = GGrayColor;
  cell.layer.cornerRadius = GLargeCornerRadius;
  cell.layer.masksToBounds = YES;
  
  return cell;
}

#pragma mark searchBar delegate
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar {
}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
}

#pragma mark - 私有方法
#pragma mark 设置导航栏
- (void)setNav {
  UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth - 54, NavHeight)];
  searchBar.tintColor = [UIColor whiteColor];
  searchBar.barStyle = UIBarStyleBlack;
  searchBar.delegate = self;
  [searchBar becomeFirstResponder];
  self.navigationItem.titleView = searchBar;
  self.navigationItem.hidesBackButton = YES;
  self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStyleDone target:self action:@selector(back)];
  [[CommonMethod sharedCommonMethod]setNavButtonItem:self.navigationItem.rightBarButtonItem type:ButtonItemType_Gray];
}

- (void)back {
  [self.navigationController popViewControllerAnimated:YES];
}

@end
